
-- Test program (lanes3.lua):

-- From Stefan, looping _sometimes_ stops before getting to 10000. Without
-- an error message.  Why???

require "lanes"

-- produce
local pp = lanes.prepare"*" (
 function (channel)
  local PRINT= function(...) io.stderr:write( table.concat{...} .. "\n" ) end
  require "lanes"
  local ch = lanes.tube(channel)
  local i = 0
  while i < 99999 do
    i = i + 1
    ch:send(i)
--PRINT( "P: ", i, " sent" )
    --if i % 1 == 0 then print(i) end
    local _ = ch:receive()
--PRINT( "\t\t\tP: ", _, " received" )
  end
  PRINT("==> ", i)
end )

-- consume
local cc= lanes.prepare"*" (
 function (channel)
  local PRINT= function(...) io.stderr:write( table.concat{...} .. "\n" ) end
  require "lanes"
  local ch = lanes.tube(channel)
  while true do
    local k = ch:receive()
--PRINT( "\tC: ", k, " received" )
    ch:send(k)
--PRINT( "\t\tC: ", k, " sent" )
  end
end )

local p = pp("tube")
local c = cc("tube")

-- Wait for p to finish (c remains running)
--
local _= p[0]

--
print( p.state )
assert( p.state == "done" )
